﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using PM.Controls;
using System.Windows.Media.Animation;
using PM.Controls.Common;
using HealthManagement.Data.Service;
using HealthManagement.Data.Model;
using System.Windows.Navigation;
using HealthManagement.Model;

namespace HealthManagement
{
    /// <summary>
    /// CardLogin.xaml 的交互逻辑
    /// </summary>
    public partial class CardLogin : PMWindow
    {
        Frame frame;
        public CardLogin()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(LoginWindow_Loaded);
            this.KeyDown += new KeyEventHandler(CardLogin_KeyDown);
            this.UserIdBox.Focus();
        }
        public CardLogin(Frame parent) : this()
        {
            this.frame = parent;
            
           
        }

        void CardLogin_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                RadButton_Click(this, null);
                return;
            }


            if (e.Key == Key.Escape)
            {
                CloseLoginWindow();
                return;
            }
        }

        void LoginWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Storyboard sb = this.Resources["sbProliferation"] as Storyboard;
            sb.Begin();
        }

        public void CloseLoginWindow()
        {
            this.CloseWindow();
        }

        private void RadButton_Click(object sender, RoutedEventArgs e)
        {
            UserService ser = new UserService();

            if (UserIdBox.Value == null)
            {
                Tools.ShowMessage(false, "用户名为空，请重新输入！");
                return;
            }
            string idcard = UserIdBox.Value.ToString().Trim();

            var obj = ser.LoadMeasureUserList(c => c.IdCard == idcard).FirstOrDefault();

            if (obj != null)
            {
                MeasureUser user = obj as MeasureUser;
                UserModel model = new UserModel()
                {
                    UserId = user.userId,
                    Name = user.username,
                    Photo = "/Images/EmpID" + 1 + ".jpg",
                    Mobile = user.MobilePhoneNumber,
                    Address = user.Address,
                    Age = user.Age,
                    Birthday = user.Birthday == null ? default(DateTime).ToShortDateString()
                    : user.Birthday.Value.ToShortDateString(),
                    Email = user.email,
                    Height = user.Height,
                    BloodType = user.BloodType,
                    IdCard = user.IdCard

                };

                Application.Current.Properties["selectedUser"] = model;
                frame.Source = new Uri("View/MeterPanel.xaml", UriKind.Relative);
                this.Close();
                

            }
            else
            {
                Tools.ShowMessage(false, "服务编号不存在！");
            }
        }

        private void cancel_click(object sender, RoutedEventArgs e)
        {
            UserIdBox.Value = null;
        }

    }
}
